package com.foot.travel.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.foot.travel.R;
import com.foot.travel.event.NetRefreshEvent;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.MyRequest;
import com.koalac.ironman.net.ResponseCallback;
import com.koalac.ironman.net.pojo.request.GetMyDetail;
import com.koalac.ironman.net.pojo.request.GetUserDetail;
import com.koalac.ironman.net.pojo.response.Response;
import com.koalac.ironman.net.pojo.response.User;
import com.koalac.ironman.util.ScreenInfoUtil;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.koalac.ironman.widget.LifeqImageView;
import com.koalac.ironman.widget.LoadingDialog;
import com.squareup.otto.Subscribe;

public class MyActivity extends ModuleActivity implements OnClickListener {

	@ViewInject(id = R.id.cover)
	LifeqImageView mCoverImageView;
	@ViewInject(id = R.id.nickname)
	TextView mNicknameView;
	@ViewInject(id = R.id.class_name)
	TextView mClassNameView;

	@ViewInject(id = R.id.account_name)
	TextView mAccountNameView;
	@ViewInject(id = R.id.where_name)
	TextView mWhereNameView;
	@ViewInject(id = R.id.introduce)
	TextView mIntroduceView;

	@ViewInject(id = R.id.hobby)
	TextView mHobbyView;
	@ViewInject(id = R.id.my_travel_layout, click = "onClick")
	TextView mMyTravelLayout;

	@ViewInject(id = R.id.parent_view)
	View mParentView;

	public static final String NAV_TITLE = "nav_title";
	public static final String IS_MY = "is_my";
	
	public static final String UID_KEY = "uid_key";

	GetMyDetail mGetMyDetail;
	User mUser;
	public static final int UPDATE_MY_INFO_REQUEST_CODE = 1 << 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		String title = getIntent().getStringExtra(NAV_TITLE);
		setNavigationTitle(title);
		setBackButtonVisibility(true);
		mParentView.setVisibility(View.INVISIBLE);
		setContentLayoutBackgroudColor(getResources().getColor(R.color.module_color1));
		setCoverViewHeight();
		if (getIntent().getBooleanExtra(IS_MY, false)) {
		    mEditMyInfoView.setVisibility(View.VISIBLE);
			mEditMyInfoView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (mUser == null) {
						toast("获取个人资料失败!");
						return;
					}
					Intent intent = new Intent(MyActivity.this, UpdateMyActivity.class);
					intent.putExtra(UpdateMyActivity.USER_DATA, mUser);
					startActivityForResult(intent, UPDATE_MY_INFO_REQUEST_CODE);
				}
			});
			initMyHomepage();
		}else {
		    initOtherHomepage();
		    mMyTravelLayout.setVisibility(View.GONE);
		}
	}

	private void initOtherHomepage() {
	    GetUserDetail getUserDetail = new GetUserDetail();
	    getUserDetail.setUser_id(getIntent().getStringExtra(UID_KEY));
	    final LoadingDialog loadingDialog = showLoadingDialog("获取个人信息...");
	    MyRequest.request(MyActivity.this, getUserDetail, new ResponseCallback() {
	        
	        @Override
	        public void process(boolean result, Context context, Response data,
	    	    String msg) {
	            canceledLoadingDialog(loadingDialog);
	            if (result) {
	        	mParentView.setVisibility(View.VISIBLE);
			mUser = data.getObject(User.class);
			steupUI();
			   setNetworkVisibility(false);
			}else {
			    setNetworkVisibility(true);
			}
	        }
	    });
	}
	
	  @Subscribe
	    public void onRefresh(NetRefreshEvent netRefreshEvent) {
		setNetworkVisibility(false);
		if (getIntent().getBooleanExtra(IS_MY, false)) {
		    initMyHomepage();
		}else {
		    initOtherHomepage();
		}
	    }
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == UPDATE_MY_INFO_REQUEST_CODE) {
				mUser = (User) data.getSerializableExtra(UpdateMyActivity.USER_DATA);
				steupUI();
		}
	}
	
	private void setCoverViewHeight() {
		int height = ScreenInfoUtil.getScreenWidth(MyActivity.this);
		RelativeLayout.LayoutParams layoutParams = (android.widget.RelativeLayout.LayoutParams) mCoverImageView
				.getLayoutParams();
		layoutParams.height = height;
		mCoverImageView.setLayoutParams(layoutParams);
	}

	private void initMyHomepage() {
		mGetMyDetail = new GetMyDetail();
		final LoadingDialog loadingDialog = showLoadingDialog("获取个人信息...");
		MyRequest.request(MyActivity.this, mGetMyDetail, new ResponseCallback() {

			@Override
			public void process(boolean result, Context context, Response data, String msg) {
				canceledLoadingDialog(loadingDialog);
				if (result) {
					mParentView.setVisibility(View.VISIBLE);
					mUser = data.getObject(User.class);
					steupUI();
					   setNetworkVisibility(false);
				}else {
				    setNetworkVisibility(true);
				}
			}
		});
	}
	
	private void steupUI(){
		mCoverImageView.loadFile(mUser.getHead_img_url(), ImageType.SourceImage, null, null);
		String sex = mUser.getSex();
		if (sex.equals(User.SEX_MALE)) {
			Drawable rightDrawable = getResources().getDrawable(R.drawable.icon_personal_man);
			rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(),
					rightDrawable.getMinimumHeight());
			mNicknameView.setCompoundDrawables(null, null, rightDrawable, null);
		}
		if (sex.equals(User.SEX_FEMALE)) {
			Drawable rightDrawable = getResources().getDrawable(R.drawable.icon_personal_women);
			rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(),
					rightDrawable.getMinimumHeight());
			mNicknameView.setCompoundDrawables(null, null, rightDrawable, null);
		}
		
		if (StringUtils.isEmpty(mUser.getAccount())) {
		    mNicknameView.setText("尚未填写");
		}else {
		    mNicknameView.setText(mUser.getAccount());
		}
		
		if (StringUtils.isEmpty(mUser.getClasses())) {
		    mClassNameView.setText("尚未填写");
		}else {
		    mClassNameView.setText(mUser.getClasses());
		}

		if (StringUtils.isEmpty(mUser.getUser_name())) {
		    mAccountNameView.setText("尚未填写");
		}else {
		    mAccountNameView.setText(mUser.getUser_name());
		}
		
		if (StringUtils.isEmpty(mUser.getRegion())) {
		    mWhereNameView.setText("尚未填写");
		}else {
		    mWhereNameView.setText(mUser.getRegion());
		}
		
		if (StringUtils.isEmpty(mUser.getMessage())) {
		    mIntroduceView.setText("尚未填写");
		}else {
		    mIntroduceView.setText(mUser.getMessage());
		}
		
		
		if (StringUtils.isEmpty(mUser.getHobby())) {
		    mHobbyView.setText("尚未填写");
		}else {
		    mHobbyView.setText(mUser.getHobby());
		}
		
	}

	@Override
	protected void initLayout(ViewGroup rootContentView) {
		addContentView(R.layout.activity_my);
	}

	@Override
	public void onClick(View v) {
		int viewID = v.getId();
		switch (viewID) {
		case R.id.my_travel_layout:
			startActivity(new Intent(MyActivity.this, MyGalleryListActivity.class));
			break;
		}
	}

}
